package com.example.goods_service.controller;

import com.example.goods_service.config.OSSClientUtil;
import com.example.goods_service.otherservice.OrderService;
import com.example.goods_service.return_data.Mall_Data;
import com.example.goods_service.service.GoodsService_zhoufuyu;
import com.example.goods_service.vo.T_attribute;
import com.example.goods_service.vo.T_com_com_all;
import com.example.goods_service.vo.T_config;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@Api

public class GoodsController_zhoufuyu {
    @Autowired
    GoodsService_zhoufuyu goodsService_zhoufuyu;
    @Autowired
    OSSClientUtil ossClientUtil;
    //上传的图片
    String goodspicture;
    //定义图片id为全局变量
    int picture_id;
    //商品id
    int goodsid;

    @Autowired
    OrderService orderService;

    Mall_Data mall_data = new Mall_Data();


    @ApiOperation("修改商品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commodity_name", value = "商品名称"),
            @ApiImplicitParam(name = "commodity_description", value = "商品描述"),
            @ApiImplicitParam(name = "commodity_price", value = "商品价格"),
            @ApiImplicitParam(name = "sales_volume", value = "商品销量"),
            @ApiImplicitParam(name = "id", value = "商品id")
    })
    //修改商品信息
    @GetMapping("/upgoods")
    public Mall_Data upgoods(@RequestParam("commodity_name") String commodity_name,
                             @RequestParam("commodity_description") String commodity_description,
                             @RequestParam("commodity_price") double commodity_price,
                             @RequestParam("id") int id) {
        int x = goodsService_zhoufuyu.upgoods(commodity_name, commodity_description, commodity_price, id);
        if (x>0) {
            mall_data.setcode(1);
            mall_data.seterror("修改商品信息");
            mall_data.setdata(x);
        }
        return mall_data;
    }

    @ApiOperation("修改商品销量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品id"),
            @ApiImplicitParam(name = "数量", value = "订单上的数量"),
    })
    //根据商品信息修改销量
    @GetMapping("/upxl")
    public Mall_Data upxl(@RequestParam("id") int id,
                          @RequestParam("shuliang") int shuliang
                          ) {
       int x =goodsService_zhoufuyu.upxl(id,shuliang);
       if (x>0){
           mall_data.setcode(1);
           mall_data.seterror("修改商品销量");
           mall_data.setdata(x);
       }
        return mall_data;
    }


    @ApiOperation("根据规格修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "attribute", value = "商品详情"),
            @ApiImplicitParam(name = "price", value = "商品价格"),
            @ApiImplicitParam(name = "stock", value = "库存"),
            @ApiImplicitParam(name = "id", value = "商品id")
    })
    @GetMapping("/ggupgoods")
    public Mall_Data ggupgoods(@RequestParam("attribute") String attribute,
                             @RequestParam("price") double price,
                             @RequestParam("stock") int stock,
                             @RequestParam("id") int id) {

        int x = goodsService_zhoufuyu.updategg(attribute, price, stock, id);

        if (x>0) {
            mall_data.setcode(1);
            mall_data.seterror("根据规格修改");
            mall_data.setdata(x);
        }
        return mall_data;
    }


    @ApiOperation("根据订单信息修改库存")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commodity_id", value = "商品id"),
            @ApiImplicitParam(name = "attribute", value = "商品规格"),
            @ApiImplicitParam(name = "shuliang", value = "数量")
    })
    @GetMapping("/upkc")
    public Mall_Data upkc(@RequestParam("commodity_id") int commodity_id,
                          @RequestParam("attribute") String attribute,
                          @RequestParam("shuliang") int shuliang) {

        int x = goodsService_zhoufuyu.upkc(commodity_id, attribute, shuliang);

        if (x>0) {
            mall_data.setcode(1);
            mall_data.seterror("根据订单自动修改库存");
            mall_data.setdata(x);
        }
        return mall_data;
    }


    @ApiOperation("根据id查询商品类目")
    @ApiImplicitParam(name = "id", value = "商品id")
    @GetMapping("/selallpeizhi")
    public Mall_Data selallpeizhi(@RequestParam("id") int id) {
        String peizhi = goodsService_zhoufuyu.selallpeizhi(id);

        if (peizhi != "") {
            mall_data.setcode(1);
            mall_data.seterror("查询类目成功");
            mall_data.setdata(peizhi);
        }
        return mall_data;
    }

    @ApiOperation("根据id查询所有属性")
    @ApiImplicitParam(name = "id", value = "商品id")
    @GetMapping("/selallshuxing")
    public Mall_Data selallshuxing(@RequestParam("id") int id) {

        List<T_attribute> shuxing = goodsService_zhoufuyu.sel_attribute(id);
        goodsid = id;
        String pjsx = "";
        for (int i = 0; i < shuxing.size(); i++) {
            pjsx += shuxing.get(i).getAttribute() + ":" + shuxing.get(i).getDetails() + "  ";
        }
        System.out.println(pjsx);

        if (shuxing != null) {
            mall_data.setcode(1);
            mall_data.seterror("查询属性成功");
            mall_data.setdata(pjsx);
        }
        return mall_data;
    }

    @ApiOperation("根据id查询所有详情")
    @ApiImplicitParam(name = "id", value = "商品id")
    @GetMapping("/selallxiangqing")
    public Mall_Data selallxiangqing(@RequestParam("id") int id) {
        List<T_config> xiangqing = goodsService_zhoufuyu.sel_config(id);
        String pjxq = "";
        for (int i = 0; i < xiangqing.size(); i++) {
            pjxq += xiangqing.get(i).getConfiguration_type() + ":" + xiangqing.get(i).getConfig_details() + "  ";
        }
        System.out.println(pjxq);
        if (xiangqing != null) {
            mall_data.setcode(1);
            mall_data.seterror("查询详情成功");
            mall_data.setdata(pjxq);
        }
        System.out.println(pjxq);
        return mall_data;
    }

    //查询所有
    @ApiOperation("根据id查询商品信息")
    @ApiImplicitParam(name = "id", value = "商品id")
    @GetMapping("/selbyid")
    public Mall_Data selall(@RequestParam("id") int id) {
        List<T_com_com_all> spxx = goodsService_zhoufuyu.selbyid(id);
        for (int i = 0; i < spxx.size(); i++) {
            System.out.println("查询商品图片=" + spxx.get(i).getCommodity_picture());
        }

        if (spxx != null) {
            mall_data.setcode(1);
            mall_data.seterror("查询商品信息成功");
            mall_data.setdata(spxx);
        }
        return mall_data;
    }

    @ApiOperation("添加商品图片")
    @ApiImplicitParam(name = "image", value = "图片")
    @RequestMapping("/addpic")
    public Mall_Data addpicture2(@RequestParam MultipartFile image, HttpServletRequest request) {
        System.out.println("上传图片--------" + image);
        //上传图片
        ossClientUtil.checkImage(image);
        //得到你上传的图片连接url
        String[] strArr = ossClientUtil.checkImage(image).split("\\,");
        goodspicture = strArr[0];
        for (int i = 0; i < strArr.length; ++i) {
            //图片路径
            System.out.println(strArr[i]);
            //变量保存图片url
            String url = strArr[i];
            //将保存的图片路径添加到单个的图片表
            int n = goodsService_zhoufuyu.addpic(url);
            //定义一个全局变量 根据url得到图片id，查你添加图片的id
            picture_id = goodsService_zhoufuyu.selpicid(url);
            //去修改商品图片表
            int n2 = goodsService_zhoufuyu.upcom_pic(picture_id, goodsid);

            System.out.println("图片添加成功");
        }
        return mall_data.setdata("图片添加成功");
    }


}
